#include "cumsum_tiling.h"
#include "register/op_def_registry.h"
#include "tiling/platform/platform_ascendc.h"
namespace optiling
{
    const uint32_t BLOCK_SIZE = 32;
    static ge::graphStatus TilingFunc(gert::TilingContext *context)
    {

        CumsumTilingData tiling;
        uint32_t sizeofdatatype;
        uint32_t stepsize = 1;
        uint32_t dim_num;
        uint32_t datatypeclass = 0;
	int32_t  exclusive = *context->GetAttrs()->GetBool(0);
	int32_t reverse = *context->GetAttrs()->GetBool(1);
        auto dt = context->GetInputTensor(0)->GetDataType();
        auto xshape = context->GetInputShape(0)->GetOriginShape();
        dim_num = xshape.GetDimNum();
        auto totalLength = context->GetInputShape(0)->GetOriginShape().GetShapeSize();
	int64_t shape[128];
	int64_t *ss = &shape[0];
	for (int i = 0; i < dim_num; i++) {
            ss[i] = xshape.GetDim(i);
	}
	tiling.set_shape(shape);

        if (dt == 0 || dt == 3)
        {
            sizeofdatatype = 4;
        }
        else if (dt == 1)
        {
            sizeofdatatype = 2;
        }
        else if (dt == 2)
        {
            sizeofdatatype = 1;
        }
        context->SetBlockDim(1);
        tiling.set_exclusive(exclusive);
	tiling.set_reverse(reverse);
	printf("totalLength: %ld \n",totalLength);
	tiling.set_blockLength(totalLength);
        tiling.set_dimnum(dim_num);
	tiling.SaveToBuffer(context->GetRawTilingData()->GetData(), context->GetRawTilingData()->GetCapacity());
   	context->GetRawTilingData()->SetDataSize(tiling.GetDataSize());
    	size_t *currentWorkspace = context->GetWorkspaceSizes(1);
    	currentWorkspace[0] = 0;
        return ge::GRAPH_SUCCESS;
    }
}

namespace ge
{
    static ge::graphStatus InferShape(gert::InferShapeContext *context)
    {
        const gert::Shape *x1_shape = context->GetInputShape(0);
        gert::Shape *y_shape = context->GetOutputShape(0);
        *y_shape = *x1_shape;
        return GRAPH_SUCCESS;
    }
}

namespace ops
{
    class Cumsum : public OpDef
    {
    public:
        explicit Cumsum(const char *name) : OpDef(name)
        {
	this->Input("x")
	.ParamType(REQUIRED)
	.DataType({ge::DT_FLOAT16, ge::DT_FLOAT, ge::DT_INT32, ge::DT_INT8})
	.Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
	.UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
	this->Input("axis")
	.ParamType(REQUIRED)
	.DataType({ge::DT_INT32, ge::DT_INT32, ge::DT_INT32, ge::DT_INT32})
	.Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
	.UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
	this->Output("y")
	.ParamType(REQUIRED)
	.DataType({ge::DT_FLOAT16, ge::DT_FLOAT, ge::DT_INT32, ge::DT_INT8})
	.Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
	.UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
           this->Attr("exclusive").AttrType(OPTIONAL).Bool(false);
            this->Attr("reverse").AttrType(OPTIONAL).Bool(false);

            this->SetInferShape(ge::InferShape);

            this->AICore()
                .SetTiling(optiling::TilingFunc);
            this->AICore().AddConfig("ascend310b");
        }
    };

    OP_ADD(Cumsum);
}
